package br.edu.ufcg.dsc.services;

import java.util.List;

import javax.persistence.EntityTransaction;

import br.edu.ufcg.dsc.domain.Library;

public class LibraryService extends BaseService{

	public static void persist(Library p){
		EntityTransaction tx = entity.getTransaction();

		tx.begin();
		try {
			if (find(p.getName()) != null)
				entity.merge(p);
			else
				entity.persist(p);
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			e.printStackTrace();
		} 
	}
	
	public static Library find(String name){
		return entity.find(Library.class, name);
	}

	@SuppressWarnings("unchecked")
	public static List<Library> findAll() {
		return (List<Library>) entity.createQuery
		("SELECT l FROM br.edu.ufcg.dsc.domain.Library l").getResultList();
	}
	
	@SuppressWarnings("unchecked")
	public static List<Library> findByLibrarian(String userLogin){
		return (List<Library>) entity.createQuery
		("SELECT l FROM br.edu.ufcg.dsc.domain.Library l WHERE l.librarians.login LIKE :userLogin").setParameter("userLogin", userLogin).getResultList();
	}
}
